package com.window;

import com.event.FunctionEvent;
import com.event.ManageEvent;
import com.event.WindowsEven;

import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableColumn;
import javax.swing.table.TableColumnModel;
import java.awt.*;
import java.util.Vector;

public class Manage extends JFrame {
    FlowLayout flowlayout;//定义一个布局
    JLabel bgimg;//定义背景
    JMenuBar menubar;//定义菜单条
    JMenu menu, menu1;//定义菜单
    JMenuItem item1, item2, item3, item4;//定义菜单内容
    JMenuItem item_1, item_2;//定义菜单内容
    JLabel name, age, grade, number, numbercondition;
    public static JTextField nametext, agetext, gradetext, numbertext, numberconditiontext;//定义文本框
    public static JTextArea resultText;//设置文本区域显示结果
    JButton add, del, change, show, reset;//定义增查改删
    JPanel jpanel, jpanel1;
    public static ButtonGroup group;//设置按钮组
    public static JRadioButton Man, Woman, allSex;//设置单选按钮
    JTable tableL = null;//定义表格
    JScrollPane jScrollPane;//定义滑动窗格
    Object columns[] = {"姓名", "性别", "年龄", "班级", "学号"};
    static Vector rwo;
    public static DefaultTableModel model;
    public static TableColumnModel columnModel;
    final int WIDTH = 800;//框架宽度
    final int HEIGHT = 705;//框架高度

    public Manage() {
        super("学生信息管理系统");
        init();
        setVisible(true);//设置窗口可视化
        setResizable(false);//设置窗口大小不可变
        setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);//设置默认关闭方式
        validate();//让组件生效
        setName();
        allevent();
    }

    void init() {
        this.setLayout(null);//设置布局为空
        flowlayout = new FlowLayout(FlowLayout.CENTER);//布局居中
        //设置窗口大小与位置
        Toolkit kit = Toolkit.getDefaultToolkit();
        Dimension screenSize = kit.getScreenSize();
        int width = screenSize.width;
        int height = screenSize.height;
        int x = (width - WIDTH) / 2;
        int y = (height - HEIGHT) / 2;
        this.setBounds(x, y, WIDTH, HEIGHT);//设置窗口位置与大小

        //添加盘子
        jpanel = new JPanel();//初始化盘子
        jpanel.setBorder(BorderFactory.createTitledBorder("学生基本信息处理"));//显示边框
        jpanel.setBounds(0, 100, WIDTH - 8, 100);//设置盘子1位置与大小
        jpanel.setLayout(flowlayout);//设置盘子内容居中布局
        jpanel.setOpaque(false);//设置盘子透明

        //添加表格盘子
        jpanel1 = new JPanel();//初始化盘子
        jpanel1.setBorder(BorderFactory.createTitledBorder("学生信息显示"));//显示边框
        jpanel1.setBounds(0, 200, WIDTH - 8, 280);//设置盘子2位置与大小
        jpanel1.setLayout(flowlayout);//设置盘子内容居中布局
        jpanel1.setOpaque(false);//设置盘子透明

        //设置背景图片
        ImageIcon img = new ImageIcon("src/img/3.jpg");//将图片放到img变量中
        bgimg = new JLabel(img);    //初始化bgimg
        bgimg.setBounds(0, 0, img.getIconWidth(), 100);//设置背景图片位置与大小

        //添加菜单栏
        Font f = new Font("宋体", Font.BOLD, 18);//初始化字体
        menubar = new JMenuBar();//初始化菜单条
        menu = new JMenu("管理");//初始化菜单
        menu.setFont(f);//设置菜单字体风格
        menu.setForeground(Color.RED);//设置菜单字体颜色
        menu1 = new JMenu("账号");//初始化菜单
        menu1.setFont(f);//设置菜单字体风格
        menu1.setForeground(Color.RED);//设置菜单字体颜色
        item1 = new JMenuItem("查看在线员工信息", new ImageIcon("src/img/1.png"));
        item1.setFont(f);
        item2 = new JMenuItem("查看所有员工信息", new ImageIcon("src/img/2.png"));
        item2.setFont(f);
        item3 = new JMenuItem("更改员工账号信息", new ImageIcon("src/img/3.png"));
        item3.setFont(f);
        item4 = new JMenuItem("更改当前账号密码", new ImageIcon("src/img/4.png"));
        item4.setFont(f);
        item_1 = new JMenuItem("退出", new ImageIcon("src/img/5.png"));
        item_1.setFont(f);
        item_2 = new JMenuItem("注销", new ImageIcon("src/img/6.png"));
        item_2.setFont(f);

        //设置姓名
        name = new JLabel("姓名");//初始化
        name.setFont(f);
        nametext = new JTextField(10);
        nametext.setFont(f);

        //设置性别
        group = new ButtonGroup();//初始化按钮组
        Man = new JRadioButton("男");//初始化单选按钮
        Man.setFont(f);
        Woman = new JRadioButton("女");//初始化单选按钮
        Woman.setFont(f);
        allSex = new JRadioButton("全部");//初始化单选按钮
        allSex.setFont(f);

        //设置年龄
        age = new JLabel("年龄");
        age.setFont(f);
        agetext = new JTextField(8);
        agetext.setFont(f);

        //设置班级
        grade = new JLabel("班级");
        grade.setFont(f);
        gradetext = new JTextField(8);
        gradetext.setFont(f);

        //设置学号
        number = new JLabel("学号");
        number.setFont(f);
        numbertext = new JTextField(10);
        numbertext.setFont(f);

        //设置学号条件
        numbercondition = new JLabel("输入学号");
        numbercondition.setFont(f);
        numberconditiontext = new JTextField(10);
        numberconditiontext.setFont(f);

        //设置增查改删按钮
        add = new JButton("增加数据");
        add.setFont(f);
        del = new JButton("删除数据");
        del.setFont(f);
        change = new JButton("修改数据");
        change.setFont(f);
        show = new JButton("查询数据");
        show.setFont(f);
        reset = new JButton("重置数据");
        reset.setFont(f);

        table();

        //设置文本域
        resultText = new JTextArea();
        resultText.setBounds(8, 485, WIDTH - 25, 150);
        resultText.setBorder(BorderFactory.createTitledBorder("账号信息显示"));//显示边框
        resultText.setEditable(false);//不可以编辑

        //添加操作
        menu.add(item1);
        menu.add(item2);
        menu.add(item3);
        menu.add(item4);
        menu1.add(item_1);
        menu1.add(item_2);
        menubar.add(menu);
        menubar.add(menu1);
        group.add(Man);
        group.add(Woman);
        group.add(allSex);
        jpanel.add(name);
        jpanel.add(nametext);
        jpanel.add(Man);
        jpanel.add(Woman);
        jpanel.add(allSex);
        jpanel.add(age);
        jpanel.add(agetext);
        jpanel.add(grade);
        jpanel.add(gradetext);
        jpanel.add(number);
        jpanel.add(numbertext);
        jpanel.add(numbercondition);
        jpanel.add(numberconditiontext);
        jpanel.add(add);
        jpanel.add(del);
        jpanel.add(change);
        jpanel.add(show);
        jpanel.add(reset);
        jpanel1.add(jScrollPane);
        this.add(jpanel);
        this.add(jpanel1);
        this.add(resultText);
        this.add(bgimg);
        this.add(menubar);
        this.setJMenuBar(menubar);
    }

    void table() {
        tableL = getTable();
        jScrollPane = new JScrollPane(tableL);//初始化窗格浏览条
        jScrollPane.setPreferredSize(new Dimension(WIDTH - 30, 250));//给窗格设置大小
        tableL.setPreferredSize(new Dimension(WIDTH - 30, 1000));//给表格设置大小
        jScrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);//将滑动组件显示在窗口中
    }

    JTable getTable() {
        if (tableL == null) {
            tableL = new JTable();
            int[] columnWidth = {150, 150, 150, 150, 150};//设置列宽
            model = new DefaultTableModel() {
                public boolean isCellEditable(int row, int column) {
                    return false;
                }
            };//让表格不可以编辑
            model.setColumnIdentifiers(columns);
            tableL.setModel(model);//设置为表格模式
            columnModel = tableL.getColumnModel();//获取表格的控制
            tableL.getTableHeader().setReorderingAllowed(false);//让表格不可以拖到
            tableL.getTableHeader().setResizingAllowed(false);//让表格不可以拖到
            int count = columnModel.getColumnCount();//返回列数与行数
            for (int i = 0; i < count; i++) {
                TableColumn column = columnModel.getColumn(i);//返回列表对象
                column.setPreferredWidth(columnWidth[i]);
            }
            rwo = new Vector(5);
        }
        return tableL;
    }

    //定义监听
    void allevent() {
        ManageEvent listener1 = new ManageEvent();
        FunctionEvent b = new FunctionEvent();
        item1.addActionListener(listener1);
        item2.addActionListener(listener1);
        item3.addActionListener(listener1);
        item4.addActionListener(listener1);
        item_1.addActionListener(listener1);
        item_2.addActionListener(listener1);
        add.addActionListener(b);
        del.addActionListener(b);
        change.addActionListener(b);
        show.addActionListener(b);
        reset.addActionListener(b);
        WindowsEven a = new WindowsEven();//窗口关闭监听
        this.addWindowListener(a);
    }

    //定义名字
    void setName() {
        item1.setName("item1");
        item2.setName("item2");
        item3.setName("item3");
        item4.setName("item4");
        item_1.setName("item_1");
        item_2.setName("item_2");
        add.setName("add");
        del.setName("del");
        change.setName("change");
        show.setName("show");
        reset.setName("reset");
    }

}
